﻿using Microsoft.Xna.Framework;
using System;
using Terraria;
using Terraria.Audio;
using Terraria.ID;
using Terraria.ModLoader;

namespace MoreEvent.Content.Projectiles
{
    public class FriedChickenBoomerang : ModProjectile
    {
        public override void SetDefaults()
        {
            Projectile.width = 26;
            Projectile.height = 26;
            Projectile.friendly = true;
            Projectile.penetrate = -1;
            Projectile.DamageType = DamageClass.Melee;
            Projectile.scale = 1f;
        }

        public override bool TileCollideStyle(ref int width, ref int height, ref bool fallThrough, ref Vector2 hitboxCenterFrac)
        {
            width = 12;
            height = 12;
            return true;
        }
        public int customPiercing = 2;
        public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone)
        {
            Projectile.usesLocalNPCImmunity = true;
            Projectile.localNPCImmunity[target.whoAmI] = 10;
            target.immune[Projectile.owner] = 0;
            customPiercing--;
        }
        public override void OnHitPlayer(Player target, Player.HurtInfo info)
        {
            customPiercing--;
        }

        public override void AI()
        {
            if (customPiercing <= 0)
            {
                if (Projectile.ai[0] == 0f)
                {
                    Projectile.velocity.X = 0f - Projectile.velocity.X;
                    Projectile.velocity.Y = 0f - Projectile.velocity.Y;
                    Projectile.netUpdate = true;
                }
                Projectile.ai[0] = 1f;
            }
            if (Projectile.soundDelay == 0)
            {
                Projectile.soundDelay = 8;
                SoundEngine.PlaySound(SoundID.Item7, Projectile.position);
            }
            Projectile.rotation += 0.4f;
            if (Projectile.ai[1] >= 18f)
            {
                Projectile.ai[0] = 1f;
                Projectile.ai[1] = 0f;
                Projectile.netUpdate = true;
            }
            if (Projectile.ai[0] == 0f)
            {
                Projectile.ai[1]++;
            }
            else
            {
                Projectile.tileCollide = false;
                float num49 = 9f;
                float num50 = 1f;
                Vector2 vector3 = new Vector2(Projectile.position.X + Projectile.width * 0.5f, Projectile.position.Y + Projectile.height * 0.5f);
                float num51 = Main.player[Projectile.owner].position.X + Main.player[Projectile.owner].width / 2 - vector3.X;
                float num52 = Main.player[Projectile.owner].position.Y + Main.player[Projectile.owner].height / 2 - vector3.Y;
                float num53 = (float)Math.Sqrt((double)(num51 * num51 + num52 * num52));
                num53 = num49 / num53;
                num51 *= num53;
                num52 *= num53;
                if (num53 > 3000f)
                {
                    Projectile.Kill();
                }
                else
                {
                    if (Projectile.velocity.X < num51)
                    {
                        Projectile.velocity.X = Projectile.velocity.X + num50;
                        if (Projectile.velocity.X < 0f && num51 > 0f)
                        {
                            Projectile.velocity.X = Projectile.velocity.X + num50;
                        }
                    }
                    else if (Projectile.velocity.X > num51)
                    {
                        Projectile.velocity.X = Projectile.velocity.X - num50;
                        if (Projectile.velocity.X > 0f && num51 < 0f)
                        {
                            Projectile.velocity.X = Projectile.velocity.X - num50;
                        }
                    }
                    if (Projectile.velocity.Y < num52)
                    {
                        Projectile.velocity.Y = Projectile.velocity.Y + num50;
                        if (Projectile.velocity.Y < 0f && num52 > 0f)
                        {
                            Projectile.velocity.Y = Projectile.velocity.Y + num50;
                        }
                    }
                    else if (Projectile.velocity.Y > num52)
                    {
                        Projectile.velocity.Y = Projectile.velocity.Y - num50;
                        if (Projectile.velocity.Y > 0f && num52 < 0f)
                        {
                            Projectile.velocity.Y = Projectile.velocity.Y - num50;
                        }
                    }
                }
                if (Main.myPlayer == Projectile.owner)
                {
                    Rectangle rectangle = new Rectangle((int)Projectile.position.X, (int)Projectile.position.Y, Projectile.width, Projectile.height);
                    Rectangle value2 = new Rectangle((int)Main.player[Projectile.owner].position.X, (int)Main.player[Projectile.owner].position.Y, Main.player[Projectile.owner].width, Main.player[Projectile.owner].height);
                    if (rectangle.Intersects(value2))
                    {
                        Projectile.Kill();
                    }
                }
            }
        }

        public override bool OnTileCollide(Vector2 oldVelocity)
        {
            SoundEngine.PlaySound(SoundID.Dig, Projectile.position);
            Projectile.ai[0] = 1f;
            Collision.HitTiles(Projectile.position, Projectile.velocity, Projectile.width, Projectile.height);
            if (Projectile.velocity.X != oldVelocity.X)
            {
                Projectile.position.X = Projectile.position.X + Projectile.velocity.X;
                Projectile.velocity.X = -oldVelocity.X;
            }
            if (Projectile.velocity.Y != oldVelocity.Y)
            {
                Projectile.position.Y = Projectile.position.Y + Projectile.velocity.Y;
                Projectile.velocity.Y = -oldVelocity.Y;
            }
            return false; 
        }
    }
}

